package com.qiang.manager.controller;

import com.qiang.manager.domain.Teacher;
import com.qiang.manager.factory.Factory;
import com.qiang.manager.service.IteacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherControllerImp implements IteacherController {
    IteacherService ts = Factory.getTeacherService();
    Scanner sc = new Scanner(System.in);

    @Override
    public void addTeacher() {
        String id;
        lo:
        while (true) {
            System.out.println("输入工号");
            id = sc.next();
            boolean result = ts.Exit(id);
            if (result) {
                System.out.println("工号已存在，请重新输入");
                id = sc.next();
            } else {
                Teacher tea = getTeacher(id);

                ts.addTeacher(tea);
                System.out.println("添加成功");
                break lo;
            }
        }



    }

    @Override
    public void deleteTeacher() {

        String id;
        lo:
        while (true) {
            System.out.println("输入工号");
            id = sc.next();
            boolean result = ts.Exit(id);
            if (!result) {
                System.out.println("工号不存在，请重新输入");
                break lo;
            } else {
                ts.deleteTeacher(id);
                System.out.println("删除成功");
                break lo;
            }
        }


    }

    @Override
    public void updateTeacher() {

        String id;

        lo:
        while (true) {
            System.out.println("输入工号");
            id = sc.next();
            boolean result = ts.Exit(id);
            if (!result) {
                System.out.println("工号不存在，请重新输入");
                break lo;
            } else {
                Teacher newtea = getTeacher(id);

                ts.updateTeacher(id, newtea);
                System.out.println("修改成功");
                break lo;
            }
        }

    }

    private Teacher getTeacher(String id) {
        System.out.println("请输入名字");
        String name = sc.next();

        System.out.println("请输入年龄");
        int age = sc.nextInt();

        System.out.println("请输入工资");
        double gongzi = sc.nextDouble();

        //创建学生对象
        Teacher newtea = new Teacher();
        newtea.setId(id);
        newtea.setName(name);
        newtea.setAge(age);
        newtea.setGongzi(gongzi);
        return newtea;
    }

    @Override
    public void findTeacher() {
        ArrayList<Teacher> teacher = ts.findTeacher();
        if (teacher.size() == 0) {
            System.out.println("查无信息，请输入厚再试");
            return;
        }
        System.out.println("id\t\t" + "姓名\t\t" + "年龄\t\t" + "班级");
        for (int i = 0; i < teacher.size(); i++) {
            if (teacher.size() != 0) {
                System.out.println(teacher.get(i).getId() + "\t\t" + teacher.get(i).getName() + "\t" + teacher.get(i).getAge() + "\t\t" + teacher.get(i).getGongzi());
            }
        }

    }

    @Override
    public void Start() {
        lo:
        while (true) {
            System.out.println("------欢迎来到老师管理系统------");
            System.out.println("1.添加老师");
            System.out.println("2.删除老师");
            System.out.println("3.修改老师");
            System.out.println("4.查看老师");
            System.out.println("5.退出系统");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addTeacher();
                    break;
                case 2:
                    deleteTeacher();
                    break;
                case 3:
                    updateTeacher();
                    break;
                case 4:
                    findTeacher();
                    break;
                case 5:
                    break lo;
                default:
                    System.out.println("等大你的双眼看清楚再输入");
                    break;
            }
        }
    }
}
